// @Author EthanScriptOn
// @Desc
package config_source_loader

import (
	"errors"
	"gitee.com/fatzeng/srf_switch_config/config_define"
)

type ArgsBaseSourceLoader struct {
	*BasicSourceLoader
	currentLoaderSourceType config_define.ConfigSourceType
}

func GenerateArgsBaseSourceLoader(steps ConfigSourceLoaderSteps, currentLoaderSourceType config_define.ConfigSourceType) (*ArgsBaseSourceLoader, error) {
	if steps == nil {
		return nil, errors.New("steps can not be nil")
	}
	basicLoader, err := GenerateBasicSourceLoader(steps)
	if err != nil {
		return nil, err
	}
	argsBaseSourceLoader := new(ArgsBaseSourceLoader)
	argsBaseSourceLoader.BasicSourceLoader = basicLoader
	argsBaseSourceLoader.currentLoaderSourceType = currentLoaderSourceType
	return argsBaseSourceLoader, nil
}

func (a *ArgsBaseSourceLoader) ShouldExecute(interface{}) (bool, error) {
	if err := a.ArgsCanSupport(); err != nil {
		return false, err
	}
	return true, nil
}

func (a *ArgsBaseSourceLoader) ArgsCanSupport() (err error) {
	return a.BasicSourceLoader.CanSupport(a.currentLoaderSourceType)
}

func (a *ArgsBaseSourceLoader) ConvertToLine(envData map[string]string) []string {
	envDataLines := make([]string, 0)
	for key, value := range envData {
		line := key + "=" + value
		envDataLines = append(envDataLines, line)
	}
	return envDataLines
}
